package project02.controller.dataModify;

import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import project02.cool.Helper;
import project02.dao.DateDataDao;
import project02.model.DataModifyBean;
import project02.model.DateData;

public class IndexController extends Controller {

    private DateDataDao dao = new DateDataDao();
    private Helper helper = Helper.getInstance();

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(IndexController.class.getName());

    @Override
    public Navigation run() {

        DateData entity = new DateData();
        try {
            entity = dao.find(asString("dataId"));

        } catch (Exception e) {
            requestScope("test", e.getMessage());
        }

        requestScope("resultData", setBean(entity));

        return forward("index.jsp");
    }

    /**
     * 修正画面Beanにentityのデータを詰める
     * @param entity entity
     * @return 修正画面Bean
     */
    private DataModifyBean setBean(DateData entity) {

        DataModifyBean bean = new DataModifyBean();

        bean.setDataId(entity.getDataId());
        bean.setYear(entity.getYear());
        bean.setMonth(entity.getMonth());
        bean.setDate(entity.getDate());
        bean.setStartYear(helper.getYear(entity.getGoToWork()));
        bean.setStartMonth(helper.getMonth(entity.getGoToWork()));
        bean.setStartDate(helper.getDay(entity.getGoToWork()));
        bean.setStartHour(helper.getHour(entity.getGoToWork()));
        bean.setStartMinuts(helper.getMinuts(entity.getGoToWork()));
        bean.setEndYear(helper.getYear(entity.getGoHome()));
        bean.setEndMonth(helper.getMonth(entity.getGoHome()));
        bean.setEndDate(helper.getDay(entity.getGoHome()));
        bean.setEndHour(helper.getHour(entity.getGoHome()));
        bean.setEndMinuts(helper.getMinuts(entity.getGoHome()));
        bean.setGtw(entity.getGoToWork());
        bean.setGh(entity.getGoHome());

        return bean;
    }
}
